package com.six.elemo_manger.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;
import com.bumptech.glide.Glide;
import com.six.elemo_manger.R;
import com.six.elemo_manger.ui.Fra_manger;

import java.util.List;

public class MangerAdapter extends BaseAdapter {
    private List<AVObject> list;
    private Context context;


    public MangerAdapter(List<AVObject> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //将布局文件转化为View对象
        View view = LayoutInflater.from(context).inflate(R.layout.item_manger,null);

        /**
         * 找到item布局文件中对应的控件
         */
        ImageView img_manger = (ImageView) view.findViewById(R.id.img_manger);
        TextView tv_name = (TextView) view.findViewById(R.id.tv_manger_name);
        TextView tv_price = (TextView) view.findViewById(R.id.tv_manger_price);
        TextView tv_address = (TextView) view.findViewById(R.id.food_Address_manger);
        Button btn_send=view.findViewById(R.id.btn_Send);
        /**
         * 设置控件的对应属性值
         */
         Glide.with(context).load(list.get(position).getString("food_Img")).into(img_manger);
         tv_name.setText(list.get(position).getString("food_Name"));
         tv_price.setText(list.get(position).getString("food_Price"));
        // tv_address.setText(list.get(position).getString("Post_Address"));
         btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 //执行发货
                // 第一参数是 className,第二个参数是 objectId
                final AVObject todo = AVObject.createWithoutData("FoodList", list.get(position).getObjectId());
                // 修改 content
                todo.put("food_isPay",0);
                // 保存到云端
                todo.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e==null){
                            Toast.makeText(context, "发货成功", Toast.LENGTH_SHORT).show();

                        }else {
                            Toast.makeText(context, "发货失败", Toast.LENGTH_SHORT).show();

                        }
                    }
                });
            }
        });
        return view;
    }


    }




